<?php

class FicheVitrineAddModelModuleFrontController extends VitrineFrontController {
	
	public function initContent() {
		
		parent :: initContent();
		$abo = $this->checkAbo();
		
		$customer = Context::getContext()->customer;
		$message = "";

		$link = new Link();
		
		$linkModule = $link->getModuleLink('fichevitrine', 'default');
		$linkListModel = $link->getModuleLink('fichevitrine', 'list', array("type"=>Fiche::FICHE_MODELE));
		$linkListFiche = $link->getModuleLink('fichevitrine', 'list', array("type" => Fiche::FICHE));
		$linkAdd = $link->getModuleLink('fichevitrine', 'addmodel');
		$linkEditor = $link->getModuleLink('fichevitrine', 'editormodel');
		$linkAddFiche = $link->getModuleLink('fichevitrine', 'addfiche');
		$linkAddModel = $link->getModuleLink('fichevitrine', 'addmodel');
		$linkPrint = $link->getModuleLink('fichevitrine', 'print');
		
		
		$themes = FicheTheme::findAll();
		$props = FicheProp::findAll();
		$defaultFormat = Fiche::DEFAULT_FORMAT;
		$defaultName = Fiche::getDefaultModelName($customer->id);
		$message = $this->create();
		$goodTypes = FicheProp::findAll();
		$types = array();
		$types[Fiche::ALL] = 'Tous';
		$types[Fiche::FICHE] = 'Fiche';
		$types[Fiche::FICHE_MODELE] = 'Modèle';
		
		$conditions = $this->getConditionsFilters();
	
		$fiches = Fiche::findFicheBy($conditions);
		
		$currentFormat 		= ( isset($_POST['f_format']) ) 	? (int)($_POST['f_format']) 	: '-1';
		$currentType 		= ( isset($_POST['f_type']) ) 	 	? (int)($_POST['f_type']) 		: Fiche::ALL;
		$currentTheme 		= ( isset($_POST['f_theme']) ) 	 	? (int)($_POST['f_theme']) 		: -1;
		$currentOrientation = ( isset($_POST['f_orientation']) )? (int)($_POST['f_orientation']): -1;
		$currentDomain 		= ( isset($_POST['f_domain']) )		? (int)($_POST['f_domain']) 	: Fiche::ALL;
		$currentRent 		= ( isset($_POST['f_rent']) )		? (int)($_POST['f_rent']) 		: -1;
		$currentGoodType 	= ( isset($_POST['f_good']) )		? (int)($_POST['f_good']) 		: -1;
		
		$weightModulator = Fiche::WEIGHT_MODULATOR;
		$totalWeight = round(Fiche::getTotalWeight($customer) / $weightModulator , 2);
		$maxWeight = round($abo->storeSize / $weightModulator, 2);
		
		$defaultFiche = new Fiche();
		$defaultFiche->id_fiche = -1;
		$defaultFiche->name = "";
		$defaultFiche->format = "";
		$defaultFiche->orientation = -1;
		array_unshift($fiches, $defaultFiche);
		
		$this->context->smarty->assign(array(
			'goodTypes' => $goodTypes,
			'totalWeight'=>$totalWeight,
			'maxWeight'=>$maxWeight,
			'types'=>$types,
			'fiches' => $fiches,
			'currentType'=>$currentType,
			'currentDomain'=>$currentDomain,
			'currentTheme'=>$currentTheme,
			'currentGoodType'=>$currentGoodType,
			'currentOrientation'=>$currentOrientation,
			'currentRent' => $currentRent,
			'currentGoodType' 	=> $currentGoodType,
			'currentFormat'=>$currentFormat,
			'currentOrientation'=>$currentOrientation,
			'domains' => array("Tous"=>Fiche::ALL,"Public"=>Fiche::PUBLIC_FICHE,"Privé"=>Fiche::PRIVATE_FICHE),
			'linkListFiche' => $linkListFiche,
			'linkListModel' => $linkListModel,
			'linkAdd' => $linkAdd,
			'linkAddFiche' => $linkAddFiche,
			'linkAddModel' => $linkAddModel,
			'linkHome' => $linkModule,
			'linkPrint' => $linkPrint,
			'message' => $message,
			'moduleName' => 'fichevitrine',
			'defaultName' => $defaultName,
			'defaultFormat' => $defaultFormat,
			'customer' => $customer,
			'path' => 'Fiche Vitrine',
			'themes' => $themes,
			'props' => $props,
			'manual' => Fiche::getManURI(),
			'orientations' => array("paysage"=>Fiche::LANDSCAPE,"portrait"=>Fiche::PORTRAIT),
			"formats" => array_keys(Fiche::getFormats())
		));
		
		$this->setTemplate('addmodel.tpl');
	}
	
	protected function create()
	{
	
		$message = "";
		$link = new Link();
		$linkModule = $link->getModuleLink('fichevitrine', 'default');
		$linkEditor = $link->getModuleLink('fichevitrine', 'editormodel');
		$customer = Context::getContext()->customer;
		
		if ( isset($_POST['submitAddForm']) && !empty($_POST['submitAddForm']) )
		{
			$modelId = (isset($_POST['modelId'])) ? (int)($_POST['modelId']) : -1;
			$rentId = (isset($_POST['rent'])) ? (int)($_POST['rent']) : -1;
			$propId = (isset($_POST['propId'])) ? (int)($_POST['propId']) : -1;
			$public = ( isset($_POST['isPublic']) ) ? true : false;
			$format = ( isset($_POST['format']) && !empty($_POST['format']) ) ? $_POST['format'] : Fiche::DEFAULT_FORMAT;
			$orientation = ( isset($_POST['orientation']) && !empty($_POST['orientation']) ) ? $_POST['orientation'] : Fiche::LANDSCAPE;
			Fiche::addFiche(	$_POST['name'], 
								Fiche::FICHE_MODELE, 
								$customer->id, 
								$modelId, 
								$rentId, 
								$propId, 
								(int)($_POST['theme']), 
								$public,
								$format,
								$orientation);
			
			$lastInsertedFiche = Fiche::getLastFiche();
			
			if ( $lastInsertedFiche != null )
			{
				$linkEditor = $link->getModuleLink('fichevitrine', 'editormodel', array("ficheId"=>$lastInsertedFiche->id));
				Tools::redirect($linkEditor);
				//$message = "Votre fiche a bien été créée en se basant sur le modèle: ".$modelId;
			}
			else
			{
				$message = "Erreur lors de la création du modèle.";
			}
		}
		
		return $message;
	}
	
	protected function getConditionsFilters()
	{
		$customer 			= Context::getContext()->customer;
		$currentFormat 		= ( isset($_POST['f_format']) ) 	? (int)($_POST['f_format']) 	: '-1';
		$currentType 		= ( isset($_POST['f_type']) ) 	 	? (int)($_POST['f_type']) 		: Fiche::ALL;
		$currentTheme 		= ( isset($_POST['f_theme']) ) 	 	? (int)($_POST['f_theme']) 		: -1;
		$currentOrientation = ( isset($_POST['f_orientation']) )? (int)($_POST['f_orientation']): -1;
		$currentRent 		= ( isset($_POST['f_rent']) )		? (int)($_POST['f_rent']) 		: -1;
		$currentGoodType 	= ( isset($_POST['f_good']) )		? (int)($_POST['f_good']) 		: -1;
		$currentDomain 		= ( isset($_POST['f_domain']) )		? (int)($_POST['f_domain']) 	: Fiche::ALL;
		$conditions = array();
		
		//filtre sur les types de biens
		if ( isset($_POST['f_good'] ) )
		{
			$currentGoodType = (int)($_POST['f_good']);
			if( $currentGoodType != -1 )
				$conditions['propId'] = $currentGoodType;
		}
		
		// filtre sur location/vente
		if ( isset($_POST['f_rent']) )
		{
			$currentRent = (int)($_POST['f_rent']);
			if( $currentRent != -1 )
				$conditions['rent'] = $currentRent;
		}			
		
		// filtre sur l'orientation
		if ( isset($_POST['f_orientation']) )
		{
			$currentOrientation = (int)($_POST['f_orientation']);
			if( $currentOrientation != -1 )
				$conditions['orientation'] = $currentOrientation;
		}		
		
		// filtre sur le format
		if ( isset($_POST['f_format']) )
		{
			$currentFormat = $_POST['f_format'];
			if( $currentFormat != -1 )
				$conditions['format'] = $currentFormat;
		}

		//filtre sur les themes
		if ( isset($_POST['f_theme'] ) )
		{
			$currentTheme = (int)($_POST['f_theme']);
			if( $currentTheme != -1 )
				$conditions['theme'] = $currentTheme;
		}
		
		// filtre sur le type
		if ( isset($_POST['f_type']) )
		{
			$currentType = (int)($_POST['f_type']);
			if ( $currentType != Fiche::ALL )
			{
				$conditions['type'] = $currentType;
			}
		}
		
		//filtre sur le domaine publique ou privé
		if ( isset($_POST['f_domain'] ) )
		{
			if ( $currentDomain == Fiche::PRIVATE_FICHE)
			{
				$conditions['id_customer'] = (int)($customer->id);
				$conditions['public'] = Fiche::PRIVATE_FICHE;
			}
			else if( $currentDomain == Fiche::PUBLIC_FICHE )
			{
				$conditions['public'] = Fiche::PUBLIC_FICHE;
			}
			else
			{
				$conditions['id_customer'] = (int)($customer->id);
				$conditions['public'] = array(	"column" => "public",
												"operator" => "=",
												"value" => Fiche::PUBLIC_FICHE,
												"conditionnal"=>"OR");
			}
		}
		else
		{
			$conditions['id_customer'] = (int)($customer->id);
				$conditions['public'] = array(	"column" => "public",
												"operator" => "=",
												"value" => Fiche::PUBLIC_FICHE,
												"conditionnal"=>"OR");
		}
		
		
		return $conditions;
	}
}